import { HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common';
import { CreateFriendshipDto } from './dto/create-friendship.dto';
import { UpdateFriendshipDto } from './dto/update-friendship.dto';
import { PrismaService } from 'src/prisma/prisma.service';


type ExtendedFromUser = {
    fromUserId: number;
    reason: string;
    createTime: Date;
    status:number
    toUserId:number,
    fromUserInfo?: {
        username: string;
        headPic: string;
        nickName: string;
        email: string;
    };
}

type ExtendedSendUser = {
    fromUserId: number;
    reason: string;
    createTime: Date;
    toUserId:number,
    status:number
    sendUserInfo?: {
        username: string;
        headPic: string;
        nickName: string;
        email: string;
    };
}

@Injectable()
export class FriendshipService {
    @Inject(PrismaService)
    private prismaService: PrismaService;

    // 添加好友
    async add(friendAddDto: CreateFriendshipDto, userId: number) {

        const foundUser = await this.prismaService.user.findUnique({
            where: {
                username: friendAddDto.username
            }
        })

        if (!foundUser) {
            throw new HttpException("未找到该用户", HttpStatus.ACCEPTED)
        }

        if (foundUser.id === userId) {
            throw new HttpException("不能添加自己为好友", HttpStatus.ACCEPTED)
        }
        
        
        const resFriend = await this.prismaService.friendship.findMany({
            where: {
                OR: [
                    { userId: userId, friendId: foundUser.id },
                    { userId: foundUser.id, friendId: userId }
                ]
            }
        })
        console.log(resFriend);
        
        if(!!resFriend.length){
            throw new HttpException("不能重复添加好友", HttpStatus.ACCEPTED)
        }
       /* 
        检测 friendship 表里只要有 userId 和 foundUser.id 的组合 就必须return
       */
        
        return await this.prismaService.friendRequest.create({
            data: {
                fromUserId: userId,
                toUserId: foundUser.id,
                reason: friendAddDto.reason,
                status: 0
            }
        })
    }

    // 同意添加
    async agree(friendId: number, userId: number) {
        await this.prismaService.friendRequest.updateMany({
            where: {
                fromUserId: friendId,
                toUserId: userId,
                status: 0
            },
            data: {
                status: 1
            }
        })
        // 查询下好友列表 是否 已经是好友了  如果已经是好友 那么不需要重复添加
        const resFriend = await this.prismaService.friendship.findMany({
            where: { userId, friendId }
        })
        if (!resFriend.length) {
            await this.prismaService.friendship.create({
                data: { userId, friendId }
            })
        }

        return "添加成功"
    }

    // 拒绝添加
    async reject(friendId: number, userId: number) {
        await this.prismaService.friendRequest.updateMany({
            where: {
                fromUserId: friendId,
                toUserId: userId,
                status: 0
            },
            data: {
                status: 2
            }
        })
        return "拒绝添加"
    }

    // 删除好友
    async remove(friendId: number, userId: number) {
        await this.prismaService.friendship.deleteMany({
            where: {
                friendId,
                userId,

            }
        })
        return "删除成功"
    }

    // 接收的好友申请列表
    async acceptlist(userId: number) {
        const resUser: ExtendedFromUser[] = await this.prismaService.friendRequest.findMany({
            where: {
                toUserId: userId
            },
            select: {
                createTime: true,
                fromUserId: true,
                toUserId:true,
                reason: true,
                id:true,
                status:true
            }
        })

        for (const item of resUser) {
            const userInfo = await this.prismaService.user.findUnique({
                where: {
                    id: item.fromUserId
                },
                select: {
                    username: true,
                    headPic: true,
                    nickName: true,
                    email: true
                }
            })
            item.fromUserInfo = userInfo
        }
        return resUser
    }

    // 发出的好友申请
    async send(userId:number){
        const fromUser: ExtendedSendUser[] = await this.prismaService.friendRequest.findMany({
            where: {
                fromUserId: userId
            },
            select: {
                createTime: true,
                fromUserId: true,
                toUserId:true,
                reason: true,
                id:true,
                status:true
            }
        })
        for (const item of fromUser) {
            const userInfo = await this.prismaService.user.findUnique({
                where: {
                    id: item.toUserId
                },
                select: {
                    username: true,
                    headPic: true,
                    nickName: true,
                    email: true
                }
            })
            item.sendUserInfo = userInfo
        }
        return fromUser
    }
}

